package com.rsy.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * session配置
 *  1）、@EnableRedisHttpSession导入RedisHttpSessionConfiguration.class配置
 *
 *            1、给容器中添加了一个组件
 *
 *                   SessionRepository=》》》 【RedisIndexedSessionRepository】=>redis操作session.session的增删改查的封装类
 *
 *            2、SessionRepositoryFilter=》Filter: session存储过滤器,每个请求过来都必须经过filter
 *
 *                     1、创建的时候，就自动从容器中获取到了SessionRepository:
 *
 *                     2、原生的request,response都被包装。SessionRepositoryRequestWrapper,SessionRepositoryResponseWrapper
 *
 *                     3、以前获取session。 request.getSession()
 *
 *                     4、以后获取session。wrapperedRequest.getSession();===>SressionRepository中获取到
 */
@Configuration
public class GulimallSessionConfig {
    /**
     * CookieSerializer扩大session的作用域至**.gulimall.com
     * @return
     */
    @Bean
    public CookieSerializer cookieSerializer(){
        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
        cookieSerializer.setDomainName("gulimall.com");
        cookieSerializer.setCookieName("GULISESSION");
        return cookieSerializer;
    }

    /**
     * 自定义序列化
     * @return
     */
    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer(){
        return new GenericJackson2JsonRedisSerializer();
    }

}
